package com.example.platformhimybodymanage.mqrsend;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.example.platformhimybodymanage.config.rabbitmq.RabbitMQConfig;
import com.example.platformsharedcommon.entity.order.MiaoshaGoods;
import com.rabbitmq.client.Channel;

/**
 * 生产数据
 * 
 * @author Dhjie
 *
 */
@Component
public class Sender {
	private static final Logger LOGGER = LoggerFactory.getLogger(Sender.class);
	@Autowired
	private RabbitTemplate rabbitTemplate;

	/**
	 * 路由模式生产消息
	 */
	public void dirctPaySender() {

		for (int i = 0; i < 10; i++) {
			String mes = "DIRCT_QUEUE_PAY" + i;
			LOGGER.info("发送：：：：：" + mes);
			CorrelationData c = new CorrelationData(String.valueOf(i));
			rabbitTemplate.convertAndSend(RabbitMQConfig.DIRCT_EXCHANGE, RabbitMQConfig.DIRCT_QUEUE_PAY, mes, c);
		}
	}

	public void dirctShouSender() {
		for (int i = 0; i < 10000; i++) {
			String mes = "DIRCT_QUEUE_SHOU" + i;
			LOGGER.info("发送：：：：：" + mes);
			CorrelationData c = new CorrelationData(String.valueOf(i));
			rabbitTemplate.convertAndSend(RabbitMQConfig.DIRCT_EXCHANGE, RabbitMQConfig.DIRCT_QUEUE_SHOU, mes, c);
		}
	}

	/**
	 * 广播模式
	 */
	public void SendFanout() {
		for (int i = 0; i < 5; i++) {
			String mes = "RabbitMQ_test00" + i;
			LOGGER.info("发送：：：：：" + mes);
			CorrelationData c = new CorrelationData(String.valueOf(i));
			rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUT_EXCHANGE, "", mes, c);
		}
	}

	/**
	 * 广播模式
	 */
	public void Fanout(MiaoshaGoods mes) {
		String jsonString = JSONObject.toJSONString(mes);
		LOGGER.info("发送：：：：：" + jsonString);
		CorrelationData correlationData = new CorrelationData(mes.getId().toString());
		rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUT_EXCHANGE, "", jsonString,correlationData);
	}

}
